﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.InfoCollect;
using ZBJF.Warranty.Common.Enums.InsuranceProducts;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InfoCollect;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class CollectTypeCollectItemMappingsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(CollectTypeCollectItemMappingsModel model)
        {
            var total = 0;
            var list = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.GetPagingList(model, out total).Include(o => o.InsuranceProductsInfoCollectItem).Include(o => o.InsuranceProductsInfoCollectItem.InsuranceProductsInfoCollectCategory).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                CollectWithTypeName = EnumHelper.Instance.GetDescription(typeof(CollectWithType), o.CollectWithType),
                o.InsuranceProductsInfoCollectItem.InfoCollectItemName,
                o.InsuranceProductsInfoCollectItem.InsuranceProductsInfoCollectCategory.InfoCollectCategoryName
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult CollectTypeCollectItemMappingsSelector(CollectTypeCollectItemMappingsModel model)
        {
            int total;
            var list = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.GetPagingList(model, out total).Include(o => o.InsuranceProductsInfoCollectItem).Include(o => o.InsuranceProductsInfoCollectItem.InsuranceProductsInfoCollectCategory).OrderBy(o => o.CollectWithType).ThenBy(o => o.InsuranceProductsInfoCollectItem.InfoCollectCategoryID).ToList().Select(o => new InsuranceProductsInfoCollectItemMappingsModel
            {
                CollectTypeCollectItemMappingID = o.ID,
                IsMust = false,
                CanUploadPicCount = 0,
                CollectWithTypeName = EnumHelper.Instance.GetDescription(typeof(CollectWithType), o.CollectWithType),
                InfoCollectCategoryName = o.InsuranceProductsInfoCollectItem.InsuranceProductsInfoCollectCategory.InfoCollectCategoryName,
                InfoCollectItemName = o.InsuranceProductsInfoCollectItem.InfoCollectItemName
            }).ToList();

            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit(int id)
        {
            var auditProject = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.LoadByID(id);
            return View(auditProject);
        }
        [HttpPost]
        public ActionResult Save(CollectTypeCollectItemMappings model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (model.ID > 0)
            {
                if (ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.IsExist(model.CollectWithType, model.CollectItemID, model.ID))
                {
                    responseResult.Message = "该配置已存在，请修改后提交";
                }
                else
                {
                    var entity = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.LoadByID(model.ID);
                    entity.CollectWithType = model.CollectWithType;
                    entity.CollectItemID = model.CollectItemID;
                    responseResult.Success = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.UpdateEntities(entity);
                }
            }
            else
            {
                if (ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.IsExist(model.CollectWithType, model.CollectItemID))
                {
                    responseResult.Message = "该配置已存在，请修改后提交";
                }
                else
                {
                    responseResult.Success = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.AddEntities(model).ID > 0;
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add && responseResult.Success ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ID });
        }

        [HttpPost]
        public JsonResult Delete(int id)
        {
            var result = ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.Delete(id);
            return Json(new { success = result, message = result ? "删除成功！" : "删除失败！" });
        }
    }
}